<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-15
 * Time: 15:20
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\model\Admin;
use think\facade\Request;

class Store extends BaseController
{
    protected $beforeActionList = [
        'checkPrimaryScope' => ['only' => 'getStoreList,getStoreInfo']
    ];

    // 获取门店列表
    public function getStoreList($coordinate)
    {
        if($coordinate == ',') {
            $coordinate = '';
        }
        if($coordinate) {
            $coordinateArr = explode(',',$coordinate);
            $lng1 = $coordinateArr[1];
            $lat1 = $coordinateArr[0];
        }
        $list = Admin::with('img,adminUser')->order('admin.id desc')->select()->toArray();
        foreach ($list as $k=>$v) {
            if($coordinate) {
                $coordinateOneArr = explode(',', $list[$k]['coordinate']);
                if(count($coordinateOneArr) == 1) {
                    $coordinateOneArr = explode('，', $list[$k]['coordinate']);
                }
                if(count($coordinateOneArr) == 2) {
                    $lng2 = $coordinateOneArr[1];
                    $lat2 = $coordinateOneArr[0];
                    $distance = getDistance($lng1, $lat1, $lng2, $lat2);
                    $list[$k]['distance'] = $distance[0];
                    $list[$k]['distance_number'] = $distance[1];
                } else {
                    $list[$k]['distance'] = '';
                    $list[$k]['distance_number'] = 0;
                }
            } else {
                $list[$k]['distance'] = '';
                $list[$k]['distance_number'] = 0;
            }
        }
        if($coordinate) {
            $distance_number = array_column($list, 'distance_number');
            array_multisort($distance_number, SORT_ASC, $list);
            return $list;
        } else {
            return $list;
        }
    }

    // 获取门店详细信息
    public function getStoreInfo($id,$coordinate)
    {
        if($coordinate == ',') {
            $coordinate = '';
        }
        $info = Admin::with('img,adminUser')->where('id',$id)->find();
        if($coordinate) {
            $coordinateArr = explode(',',$coordinate);
            $lng1 = $coordinateArr[1];
            $lat1 = $coordinateArr[0];
            $coordinateOneArr = explode(',',$info['coordinate']);
            if(count($coordinateOneArr) == 1) {
                $coordinateOneArr = explode('，', $info['coordinate']);
            }
            if(count($coordinateOneArr) == 2) {
                $lng2 = $coordinateOneArr[1];
                $lat2 = $coordinateOneArr[0];
                $distance = getDistance($lng1, $lat1, $lng2, $lat2);
                $info['distance'] = $distance[0];
                $info['distance_number'] = $distance[1];
            } else {
                $info['distance'] = '';
                $info['distance_number'] = 0;
            }
        } else {
            $info['distance'] = '';
            $info['distance_number'] = 0;
        }
        return $info;
    }

}